################################################################################
# Libraries and Import
################################################################################
rm(list=ls())
library(cjoint)

load(file="data/dat_analysis_JP.RData")
load(file="data/charts.RData")

################################################################################
# Assign attributes and levels
################################################################################
# Attribute list
names(data_conjoint)[5:11] <- c("Yasukuni Shrine", "OBOR Cooperation", 
                                "Japan Tariff", "China Tariff",
                                "Japan Constitution", 
                                "Diaoyu Senkaku Sovereignty",
                                "Diaoyu Senkaku Dev")

# Yasukuni
attribute_list <- list()
attribute_list[["Yasukuni Shrine"]] <- c("Resume visits", 
                                         "Suspend visits (informal)", 
                                         "Suspend visits (formal)")
data_conjoint$`Yasukuni Shrine` <- factor(data_conjoint$`Yasukuni Shrine`,
       levels = c("日本の首相は靖国神社への参拝を再開する", 
                  "日本の首相は靖国神社に参拝をしないが、これは公式の取り決めではない",
                  "日本の首相は靖国神社に参拝をしないと、公式に取り決める"), 
       labels = c("Resume visits",
                  "Suspend visits (informal)",
                  "Suspend visits (formal)"))

# OBOR
attribute_list[["OBOR Cooperation"]] <- c("Non-cooperation", "Cooperation")
data_conjoint$`OBOR Cooperation` <- factor(data_conjoint$`OBOR Cooperation`,
                             levels = c("日本は、現在推進している「自由で開かれたインド太平洋戦略」を維持し、中国の「一帯一路」政策と競合する",
                                        "日本は、現在推進している「自由で開かれたインド太平洋戦略」の一部を変更し、中国の「一帯一路」政策と協調する"), 
                             labels = c("Non-cooperation", "Cooperation"))

# China Tariff
attribute_list[["Japan Tariff"]] <- c("No change", 
                                      "Increase 10%", "Increase 20%", 
                                      "Decrease 10%", "Decrease 20%")
data_conjoint$`Japan Tariff` <- factor(data_conjoint$`Japan Tariff`,
                             levels = c("日本は中国製品への現在の関税を維持する",
                                        "日本は中国製品への関税を10%引き上げる", 
                                        "日本は中国製品への関税を20%引き上げる",
                                        "日本は中国製品への関税を10%引き下げる",
                                        "日本は中国製品への関税を20%引き下げる"), 
                             labels = c("No change", 
                                        "Increase 10%", "Increase 20%", 
                                        "Decrease 10%", "Decrease 20%"))
# Japan Tariff
attribute_list[["China Tariff"]] <- c("No change", 
                                      "Increase 10%", "Increase 20%", 
                                      "Decrease 10%", "Decrease 20%")
data_conjoint$`China Tariff` <- factor(data_conjoint$`China Tariff`,
                             levels = c("中国は日本製品への現在の関税を維持する",
                                        "中国は日本製品への関税を10%引き上げる", 
                                        "中国は日本製品への関税を20%引き上げる",
                                        "中国は日本製品への関税を10%引き下げる",
                                        "中国は日本製品への関税を20%引き下げる"), 
                             labels = c("No change", 
                                        "Increase 10%", "Increase 20%", 
                                        "Decrease 10%", "Decrease 20%"))

attribute_list[["Japan Constitution"]] <- c("No amendment", "Amendment")
data_conjoint$`Japan Constitution` <- factor(data_conjoint$`Japan Constitution`,
                             levels = c("自衛隊の存在を公式に認める憲法改正を行わない", 
                                        "自衛隊の存在を公式に認める憲法改正を行う"), 
                             labels = c("No amendment", "Amendment"))

# Diayu/Senkaku Development
attribute_list[["Diaoyu Senkaku Dev"]] <- c("Status quo", "Joint development", 
                                            "China development", "Japan development")
data_conjoint$`Diaoyu Senkaku Dev` <- factor(data_conjoint$`Diaoyu Senkaku Dev`,
                             levels = c("現状を維持し、両国は尖閣諸島周辺の資源の開発を行わない", 
                                        "日中が共同で尖閣諸島周辺の資源の開発を行い、資源の分配も平等に行う", 
                                        "尖閣諸島周辺の資源の開発は中国にのみ認められる", 
                                        "尖閣諸島周辺の資源の開発は日本にのみ認められる"), 
                             labels = c("Status quo", "Joint development", 
                                        "China development", "Japan development"))

# Diayu/Senkaku Sovereignty
attribute_list[["Diaoyu Senkaku Sovereignty"]] <- c("Status quo",
                                                    "Joint sovereignty", 
                                                    "China sovereignty",
                                                    "Japan sovereignty")
data_conjoint$`Diaoyu Senkaku Sovereignty` <- factor(data_conjoint$`Diaoyu Senkaku Sovereignty`,
                                    levels = c("現状を維持し、両国は領有権を主張しない",
                                               "両国で尖閣諸島の領有権を共有する",
                                               "両国が、尖閣諸島は中国の固有の領土だと正式に認める",
                                               "両国が、尖閣諸島は日本の固有の領土だと正式に認める"), 
                                    labels = c("Status quo", 
                                               "Joint sovereignty", 
                                               "China sovereignty", 
                                               "Japan sovereignty"))

################################################################################
# Define conflicting levels
################################################################################
constraint_list <- list()

constraint_list[[1]] <- list()
constraint_list[[1]][["Diaoyu Senkaku Sovereignty"]] <- c("Joint sovereignty")
constraint_list[[1]][["Diaoyu Senkaku Dev"]] <- c("Status quo")


constraint_list[[2]] <- list()
constraint_list[[2]][["Diaoyu Senkaku Sovereignty"]] <- c("Japan sovereignty")
constraint_list[[2]][["Diaoyu Senkaku Dev"]] <- c("Joint development")

################################################################################
# Analysis
################################################################################
# Declare design
China_japandesign <- makeDesign(type='constraints', attribute.levels=attribute_list,
                                constraints=constraint_list)

# Run AMCE and print summary
summary(amce(formula = chosen ~ `China Tariff` + `Japan Constitution` + `Japan Tariff` + 
               + `OBOR Cooperation` + `Diaoyu Senkaku Sovereignty` + `Diaoyu Senkaku Dev` + `Yasukuni Shrine`,
             data = data_conjoint, design = China_japandesign, cluster=TRUE, respondent.id = "id"))

# Run AMCE regression and store results
reg_all <- amce(formula = chosen ~ `China Tariff` + `Japan Constitution` + `Japan Tariff` + 
                  + `OBOR Cooperation` + `Diaoyu Senkaku Sovereignty` + `Diaoyu Senkaku Dev` + `Yasukuni Shrine`, 
                  data = data_conjoint, design = China_japandesign, cluster=TRUE, respondent.id = "id")

################################################################################
# Plot
################################################################################
# Create conjoint plot
plot(reg_all,  
     group.order = c("OBOR Cooperation", "China Tariff", "Japan Tariff", 
                     "Yasukuni Shrine", "Japan Constitution", 
                     "Diaoyu Senkaku Sovereignty", "Diaoyu Senkaku Dev"),
     xlab = "Change in prob. of choosing",
     ggtitle("Japan"),
     size = 0.2,
     xlim = c(-0.25, 0.25),
     colors = c("grey60", "black", "grey60", "black", "black", "black", "grey60"),
     plot.theme = theme_bw() + 
     theme(panel.grid.major = element_line(colour = "grey98")) + 
     theme(legend.position="none") +
     theme(axis.title.x = element_text(size = 10)) +
     theme(plot.title = element_text(size=12)) +
     theme(plot.title = element_text(hjust = 0.5)) +
     theme(axis.ticks = element_blank()) + 
     theme(axis.text.y = element_blank()),
     text.color = "black",
     text.size = 14
)

# Save combined Japan and China chart
amce_japan_chart = last_plot()
conjoint_plot_china_japan = grid.arrange(amce_china_chart, amce_japan_chart, 
                                         ncol=2, nrow=1, widths= c(3.4,2))

ggsave("figs/2. conjoint_plot_china_japan.pdf", conjoint_plot_china_japan,
       height = 6, width = 7)

# Export to Stata for AME analysis
write.csv(data_conjoint, file = "data/conjoint_jp.csv", row.names=FALSE)